﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculateModelClasses;
using UanFileProceed;

namespace RayTracingModel
{
    public class Power
    {
        #region
        static private double GetBeta(Node node, Node node1)
        {
            double[] ftx = GetFr(node);
            double[] frx = GetFr(node1);
            double fr1 = Math.Max(ftx[0], ftx[0]);
            double fr2 = Math.Min(frx[1], frx[1]);
            double fr = fr2 - fr1;
            double ft = ftx[0] - ftx[1];
            if (ft != 0)
            {
                double result = fr / ft;
                return result;
            }
            else
            {
                throw new Exception("发射频率范围不能为0");
            }

        }
        static private double[] GetFr(Node node)
        {
            double[] fr = new double[2];
            //频率上限
            fr[0] = node.frequence + node.FrequenceWidth / 2;
            //频率下限
            fr[1] = node.frequence - node.FrequenceWidth / 2;
            return fr;
        }
        #endregion
        /// <summary>
        /// 计算每条路径的功率
        /// </summary>
        /// <param name="Rx_uan">接收机UAN文件</param>
        /// <param name="finalNode">最后一个节点</param>
        /// <param name="parentNode">倒数第二个节点</param>
        /// <returns></returns>
        static public Plural GetPower(string Rx_uan, Node finalNode, Node parentNode)
        {
            double temp = 0;
            double lamada = 300 / finalNode.frequence;
            temp = Math.Pow(lamada, 2) * GetBeta(parentNode, finalNode) / 8 * Math.PI * 377;//377欧姆是真空中的本征阻抗
            Point receiver = finalNode.Position;
            Efield tolE = finalNode.TotalE;
            Point pointbeflast = parentNode.Position;
            double ThetaAngle = ReadUan.GetThetaAngle(receiver, pointbeflast);
            double phiAngle = ReadUan.GetPhiAngle(receiver, pointbeflast);
            Plural Etheta = new Plural(tolE.X.Re * Math.Cos(ThetaAngle) * Math.Cos(phiAngle) + tolE.Y.Re * Math.Cos(ThetaAngle) * Math.Sin(phiAngle) + tolE.Z.Re * Math.Sin(ThetaAngle), tolE.X.Im * Math.Cos(ThetaAngle) * Math.Cos(phiAngle) + tolE.Y.Im * Math.Cos(ThetaAngle) * Math.Sin(phiAngle) + tolE.Z.Im * Math.Sin(ThetaAngle));
            Plural Ephi = new Plural(-tolE.X.Re * Math.Sin(phiAngle) + tolE.Y.Re * Math.Cos(phiAngle), -tolE.X.Im * Math.Sin(phiAngle) + tolE.Y.Im * Math.Cos(phiAngle));
            int index = Convert.ToInt32(Math.Floor(phiAngle * 181 + ThetaAngle));
            List<string> thetaGain = new List<string>();
            List<string> phiGain = new List<string>();
            List<string> thetaPhase = new List<string>();
            List<string> phiPhase = new List<string>();
            ReadUan.GetGainPara(Rx_uan, out thetaGain, out phiGain, out thetaPhase, out phiPhase);
            double thetagain1 = Convert.ToDouble(thetaGain[index]);
            double phigain1 = Convert.ToDouble(phiGain[index]);
            double thetaPhase1 = Convert.ToDouble(thetaPhase[index]);
            double phiPhase1 = Convert.ToDouble(phiPhase[index]);
            //E和g乘积 
            Plural thetaGainPlural = new Plural();
            thetaGainPlural.Re = Math.Sqrt(Math.Abs(Math.Pow(10, thetagain1 / 10.0))) * Math.Cos(thetaPhase1);
            thetaGainPlural.Im = Math.Sqrt(Math.Abs(Math.Pow(10, thetagain1 / 10.0))) * Math.Sin(thetaPhase1);
            Plural phiGainPlural = new Plural();
            phiGainPlural.Re = Math.Sqrt(Math.Abs(Math.Pow(10, phigain1 / 10.0))) * Math.Cos(phiPhase1);
            phiGainPlural.Im = Math.Sqrt(Math.Abs(Math.Pow(10, phigain1 / 10.0))) * Math.Sin(phiPhase1);
            double temp1Re = Etheta.Re * thetaGainPlural.Re - Etheta.Im * thetaGainPlural.Im;
            double temp1Im = Etheta.Re * thetaGainPlural.Im + Etheta.Im * thetaGainPlural.Re;
            double temp2Re = Ephi.Re * phiGainPlural.Re - Ephi.Im * phiGainPlural.Im;
            double temp2Im = Ephi.Re * phiGainPlural.Im + Ephi.Im * phiGainPlural.Re;
            double temp3Re = Math.Pow((temp1Re + temp2Re), 2) - Math.Pow((temp1Im + temp2Im), 2);
            double temp3Im = 2 * (temp1Re + temp2Re) * (temp1Im + temp2Im);
            Plural result1 = new Plural();
            result1.Re = temp * temp3Re;
            result1.Im = temp * temp3Im;
            return result1;
        }
    }
}
